package com.olmarket.core.controller;

import com.olmarket.core.api.CommonPage;
import com.olmarket.core.api.CommonResult;
import com.olmarket.core.dto.GoodParam;
import com.olmarket.core.dto.RequestsParam;
import com.olmarket.core.dto.UserSignupParams;
import com.olmarket.core.entity.*;
import com.olmarket.core.entity.constant.UserType;
import com.olmarket.core.service.StudentService;
import com.olmarket.core.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author h'zh
 * @date: 12.14
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private UserService userService;

    @Autowired
    StudentService studentService;
    
    @RequestMapping("/getUserStudentByUsername/{username}")
    public CommonResult getUserStudentByUsername(@PathVariable String username) {
        User user = userService.getUserByUsername(username);
        System.out.println(user);
        UserStudent userStudent = userService.getUserStudentByUserId(user.getUserid());
        System.out.println(userStudent);
        List<UserStudent> StudentList = new ArrayList<>();
        StudentList.add(userStudent);
        return CommonResult.success(CommonPage.restPage(StudentList));
    }

    //TODO:处理学生更新自己的信息
    @ApiOperation(value = "修改学生信息", notes = "根据当前登录学生修改学生信息")
    @RequestMapping(value = "modifyStudent", method = RequestMethod.POST)
    @Transactional
    public CommonResult modifyStudent(Principal principal, @RequestBody UserStudent userStudent) {
        int i = userService.modifyStudent(userStudent);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "得到学生", notes = "通过学生id得到学生")
    @RequestMapping("getStudentByStudentId/{studentId}")
    @Transactional
    public CommonResult getStudentByStudentId(Principal principal, @PathVariable int studentId){
        UserStudent userStudent = userService.getStudentByStudentId(studentId);
        List<UserStudent> StudentList = new ArrayList<>();
        StudentList.add(userStudent);
        return CommonResult.success(CommonPage.restPage(StudentList));
    }

    //TODO:学生添加、修改自己的商品,获取自己的商品信息
    @ApiOperation(value = "添加商品", notes = "根据当前登录学生添加商品")
    @RequestMapping(value = "addGood", method = RequestMethod.POST)
    @Transactional
    public CommonResult addGood(Principal principal, @RequestBody GoodParam goodParam) {
        int i = userService.addGood(goodParam);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "修改商品", notes = "根据当前登录学生修改商品")
    @RequestMapping(value = "modifyGood", method = RequestMethod.POST)
    @Transactional
    public CommonResult modifyGood(Principal principal, @RequestBody GoodParam goodParam) {
        int i = userService.modifyGood(goodParam);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "查询商品", notes = "查找当前学生的商品")
    @RequestMapping("getGoodList")
    @Transactional
    public CommonResult getGoodList(Principal principal) {
        User user = userService.getUserByUsername(principal.getName());
        UserStudent userStudent = userService.getUserStudentByUserId(user.getUserid());
        List<GoodInfo> goodInfoList = new ArrayList<>(userService.getGoodListByUserStudent(userStudent));
        return CommonResult.success(CommonPage.restPage(goodInfoList));
    }

    @ApiOperation(value = "删除商品", notes = "根据id删除当前学生的商品")
    @RequestMapping("deleteGood/{id}")
    @Transactional
    public CommonResult deleteGood(Principal principal,@PathVariable int id){
        int i = userService.deleteGood(id);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "商品售出", notes = "将商品状态改为已售出")
    @RequestMapping("setGoodSold/{id}")
    @Transactional
    public CommonResult setGoodSold(Principal principal,@PathVariable int id){
        int i = userService.setGoodSold(id);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }



    //TODO:学生添加、修改、删除自己的需求，获取自己的请求信息
    @ApiOperation(value = "添加需求", notes = "根据当前登录学生添加需求")
    @RequestMapping(value = "addRequest", method = RequestMethod.POST)
    @Transactional
    public CommonResult addRequest(Principal principal, @RequestBody RequestsParam requestsParam) {
        int i = userService.addRequest(requestsParam);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "修改需求", notes = "根据当前登录学生修改需求")
    @RequestMapping(value = "modifyRequest", method = RequestMethod.POST)
    @Transactional
    public CommonResult modifyRequest(Principal principal, @RequestBody RequestsParam requestsParam) {
        int i = userService.modifyRequest(requestsParam);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "查询需求", notes = "查找当前学生的需求")
    @RequestMapping("getRequestList")
    @Transactional
    public CommonResult getRequestList(Principal principal) {
        User user = userService.getUserByUsername(principal.getName());
        UserStudent userStudent = userService.getUserStudentByUserId(user.getUserid());
        List<RequestInfo> RequestInfoList = new ArrayList<>(userService.getRequestListByUserStudent(userStudent));
        return CommonResult.success(CommonPage.restPage(RequestInfoList));
    }

    @ApiOperation(value = "查询所有需求", notes = "查找所有需求")
    @RequestMapping("getAllRequests")
    @Transactional
    public CommonResult getAllRequests(Principal principal) {
        List<RequestInfo> RequestInfoList = new ArrayList<>(userService.getAllRequests());
        return CommonResult.success(CommonPage.restPage(RequestInfoList));
    }

    @ApiOperation(value = "获取所有商品", notes = "获取所有商品")
    @RequestMapping("getAllGoods")
    @Transactional
    public CommonResult getAllGoods(Principal principal) {
        List<GoodInfo> goodInfoList = new ArrayList<>(userService.getAllGoods());
        return CommonResult.success(CommonPage.restPage(goodInfoList));
    }

    @ApiOperation(value = "删除需求", notes = "根据id删除当前学生的需求")
    @RequestMapping("deleteRequest/{id}")
    @Transactional
    public CommonResult deleteRequest(Principal principal,@PathVariable int id){
        int i = userService.deleteRequest(id);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

    @ApiOperation(value = "需求已满足", notes = "将需求设为已被满足")
    @RequestMapping("setRequestSupplied/{id}")
    @Transactional
    public CommonResult setRequestSupplied(Principal principal,@PathVariable int id){
        int i = userService.setRequestSupplied(id);
        return i>0? CommonResult.success("success") : CommonResult.failed("failed");
    }

}
